﻿@using PagedList.Mvc;

@model PagedList.IPagedList<EC_ASS.Models.Product>

<table class="table table-bordered">
    <thead>
        <th>Tên</th>
        <th>Hình ảnh</th>
        <th>Số lượng</th>
        <th>Đả bán</th>
        <th>Size</th>
        <th>Giá</th>
        <th>Thao tác</th>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            if ((item.NumOfProduct - item.NumOfSold) == 0)
            {
            <tr class="error">
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    <img class="img-polaroid" src="@Url.Content("~/" + item.Picture)" height="100" width="100" title="@item.Name"/>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.NumOfProduct)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.NumOfSold)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Size)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    @Html.ActionLink("Cập nhật", "EditItem", "Admin", null, new { @class = "btn edit-product", @productID = @item.ID })
                    @Html.ActionLink("Xóa", "DeleteItem", "Admin", null, new { id = "btnDelete", @class = "btn" })
                </td>
            </tr>
            }
            else
            {
            <tr class="success">
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    <img class="img-polaroid" src="@Url.Content("~/" + item.Picture)" height="100" width="100" title="@item.Name"/>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.NumOfProduct)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.NumOfSold)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Size)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    @Html.ActionLink("Cập nhật", "EditItem", "Admin", null, new { @class = "btn edit-product", @productID = item.ID })
                    @Html.ActionLink("Xóa", "DeleteItem", "Admin", null, new { @id = "btnDelete", @class = "btn" })
                </td>
            </tr>
            }
        }
    </tbody>
</table>
<div class="row">
    <div class="pagedList pagination-centered">
        @Html.PagedListPager(Model, page => Url.Action("Index", "Admin", new { page }),
            PagedListRenderOptions.MinimalWithPageCountText)
    </div>
</div>
<!--Submit edit form-->
<script>
    $(function () {
        $('.edit-product').click(function () {
            var itemID = $(this).attr('productID');
            var editUrl = this.href + '/' + itemID;
            $('#editModalContent').load(editUrl, function () {
                $('#editModal').modal({
                    backdrop: 'static',
                    keyboard: true
                }, 'show');
                $('#editLoading').hide();
            });
            return false;
        });

        $('#editForm').submit(function (e) {
            e.preventDefault();
            $('#editLoading').show();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    $('#editLoading').hide();
                    $('#editModalContent').html(result);
                    setTimeout(function () {
                        $('#editModal').modal('hide');
                        //// Refresh:
                        $('#itemTable').load('@Url.Action("_ListItem", "Admin")').hide().fadeIn('slow');
                    }, 2000);
                }
            });
            return false;
        });
    });
</script>
